% orig code: https://zhuanlan.zhihu.com/p/471457420

function [C]=Bezier_1st(varargin)
narginchk(1,3)
ctrls=varargin{1};

%拟合
t=0:0.001:1;
Xc=(1-t).*ctrls(1,1)+t.*ctrls(2,1);
Yc=(1-t).*ctrls(1,2)+t.*ctrls(2,2);
C=[Xc' Yc'];

%如果只有一个参数：顶点矩阵;直接返回拟合的曲线
if nargin==1
    return;
else if nargin==2
        t_s=varargin{2};
        C=[Xc(int16(t_s/0.001)) Yc(int16(t_s/0.001))];
        plot(Xc,Yc,'r');
        xlabel('X','FontSize',20);
        ylabel('Y','FontSize',20);
        hold on;

        %第二个参数，比例系数t_s,将相应比例对应的拟合点显示在图表上
        plot(Xc(:,1:int16(t_s/0.001)),Yc(:,1:int16(t_s/0.001)),"g");
        hold on;
        plot(Xc(:,int16(t_s/0.001)),Yc(:,int16(t_s/0.001)),'o','LineWidth',...
            1,'MarkerEdgeColor','k','MarkerFaceColor','yellow','MarkerSize',6);
        hold on
        plot(ctrls(:,1),ctrls(:,2),'o','LineWidth',1,'MarkerEdgeColor','k',...
            'MarkerFaceColor',[rand rand rand],'MarkerSize',6);

else nargin==3 %如果有三个参数，则返回值便是对应t_s位置的拟合值，不画图
    t_s=varargin{2};
    C=[Xc(int16(t_s/0.001)) Yc(int16(t_s/0.001))];
    return ;
end

end